<?php
namespace app\common\model;

class UserGroupModel extends BaseModel
{
    protected $name = 'user_group'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    protected $type = [];

    /**
     * 添加用户角色对应关系
     * @param $utype
     * @param $uid
     * @param $groupIds
     * @return bool
     * @throws \Exception
     */
    public function addUserGroup($utype,$uid,$groupIds){
        try{
            //处理用户角色对应关系
            if($utype!='member'){
                $userGroupModel = new UserGroupModel();
                $userGroupModel->where('utype',$utype)->where('uid',$uid)->delete();
                if(!empty($groupIds)){
                    $groupDataList=[];
                    foreach($groupIds as $v){
                        $groupDataList[] = ['uid'=>$uid, 'utype'=>$utype, 'group_id'=>$v];
                    }
                    $userGroupModel->saveAll($groupDataList);
                }
            }
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }
    /**
     * 获取用户对应权限组
     * @param $uid
     * @param $utype
     * @return int|string
     */
    public function getGroupIds($uid,$utype){
        $groupIds = $this->where('uid',$uid)->where('utype',$utype)->column('group_id');
        if(empty($groupIds)){
            return 0;
        }else{
            return implode(',',$groupIds);
        }
    }
}




